package project.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import fld.background.system.controller.BaseController;
import fld.utils.DataGrid;
import fld.utils.Result;
import project.background.entity.CompanyInfo;
import project.background.entity.LeavingMessage;
import project.background.service.ActivityService;
import project.background.service.CompanyInfoService;
import project.background.service.LeavingMessageService;
import project.background.service.NewsService;
import project.background.service.ProductService;
import project.background.service.ProductTypeService;
import project.background.service.VideoService;

/**
 * web首页-济宁养殖
 * @author yc
 * 2019年1月11日
 */
@Controller
@RequestMapping (value="/web/jnyz/phone")
public class WebJnyzPhoneController extends BaseController{
	
	private final static String MODEL_PATH = "web/jnyz/phone/";
	
	private final static Long companyId = 8L;
	
	@Autowired
	private CompanyInfoService companyInfoService;
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductTypeService productTypeService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private NewsService newsService;
	@Autowired
	private VideoService videoService;
	@Autowired
	private LeavingMessageService leavingMessageService;

	/**
	 * *首页
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="index")
	public ModelAndView index(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"index");
		model.addObject("webPage", "index");
		addPublicInfo(model,0);
		//产品展示轮播
		Map<String,Object> tmap1 = new HashMap<>();
		tmap1.put("companyId", companyId);
		tmap1.put("page", 1);
		tmap1.put("rows", 18);
		DataGrid dg1 = productService.grid(tmap1);
		model.addObject("productsShow", dg1.getRows());
		
		Map<String,Object> tmap2 = new HashMap<>();
		tmap2.put("companyId", companyId);
		tmap2.put("page", 1);
		tmap2.put("rows", 6);
		DataGrid dg2 = newsService.grid(tmap2);
		model.addObject("newsShow", dg2.getRows());
		
		Map<String,Object> tmap3 = new HashMap<>();
		tmap3.put("companyId", companyId);
		tmap3.put("page", 1);
		tmap3.put("rows", 40);
		DataGrid dg3 = activityService.grid(tmap3);
		model.addObject("activityShow", dg3.getRows());
		return model;
	}
	
	/**
	 * *简介
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="about")
	public ModelAndView about(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"about");
		model.addObject("webPage", "about");
		addPublicInfo(model,1);
		return model;
	}

	/**
	 * *产品
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="product")
	public ModelAndView product(String stype){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"product");
		model.addObject("webPage", "product");
		model.addObject("stype", stype);
		addPublicInfo(model,2);
		return model;
	}
	
	/**
	 * *养殖实景/活动
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="yzsj")
	public ModelAndView yzsj(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"yzsj");
		model.addObject("webPage", "yzsj");
		addPublicInfo(model,3);
		return model;
	}

	/**
	 * *养殖技术
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="news")
	public ModelAndView news(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"news");
		model.addObject("webPage", "news");
		addPublicInfo(model,4);
		return model;
	}

	/**
	 * *养殖视频
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="yzsp")
	public ModelAndView yzsp(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"yzsp");
		model.addObject("webPage", "yzsp");
		addPublicInfo(model,5);
		return model;
	}
	
	/**
	 * *服务承诺
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="fwcn")
	public ModelAndView fwcn(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"fwcn");
		model.addObject("webPage", "fwcn");
		addPublicInfo(model,6);
		return model;
	}

	/**
	 * *联系我们
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="contact")
	public ModelAndView contact(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"contact");
		model.addObject("webPage", "contact");
		addPublicInfo(model,7);
		return model;
	}
	
	/**
	 * *公示
	 * @return
	 * @author yc
	 * @date 2018年12月9日
	 */
	@RequestMapping(value="gs")
	public ModelAndView gs(){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"gs");
		model.addObject("webPage", "gs");
		addPublicInfo(model,8);
		return model;
	}
	
	/**
	 * 查询产品列表
	 * @param tmap
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@ResponseBody
	@RequestMapping(value="productGrid")
	public DataGrid productGrid(@RequestParam Map<String,Object> tmap) {
		tmap.put("companyId", companyId);
		return productService.grid(tmap);
	}
	
	/**
	 * 详情
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@RequestMapping(value="productDetail/{id}")
	public ModelAndView productDetail(@PathVariable("id") Long id){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"productDetail");
		model.addObject("webPage", "productDetail");
		Map<String,Object> t = productService.getInfo(id);
		model.addObject("detailInfo",t);
		model.addObject("detailName",t.get("title"));
		addPublicInfo(model,2);
		return model;
	}
	
	/**
	 * *查询动态列表
	 * @param tmap
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@ResponseBody
	@RequestMapping(value="activityGrid")
	public DataGrid activityGrid(@RequestParam Map<String,Object> tmap) {
		tmap.put("companyId", companyId);
		return activityService.grid(tmap);
	}
	
	/**
	 * *动态详情
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@RequestMapping(value="activityDetail/{id}")
	public ModelAndView activityDetail(@PathVariable("id") Long id){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"activityDetail");
		model.addObject("webPage", "activityDetail");
		Map<String,Object> t = activityService.getInfo(id);
		model.addObject("detailInfo",t);
		model.addObject("detailName",t.get("title"));
		addPublicInfo(model,3);
		return model;
	}
	
	/**
	 * *查询技术列表
	 * @param tmap
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@ResponseBody
	@RequestMapping(value="newsGrid")
	public DataGrid newsGrid(@RequestParam Map<String,Object> tmap) {
		tmap.put("companyId", companyId);
		return newsService.grid(tmap);
	}
	
	/**
	 * *技术
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@RequestMapping(value="newsDetail/{id}")
	public ModelAndView newsDetail(@PathVariable("id") Long id){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"newsDetail");
		model.addObject("webPage", "newsDetail");
		Map<String,Object> t = newsService.getInfo(id);
		model.addObject("detailInfo",t);
		model.addObject("detailName",t.get("title"));
		addPublicInfo(model,4);
		return model;
	}
	
	/**
	 * *查询视频列表
	 * @param tmap
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@ResponseBody
	@RequestMapping(value="videoGrid")
	public DataGrid videoGrid(@RequestParam Map<String,Object> tmap) {
		tmap.put("companyId", companyId);
		return videoService.grid(tmap);
	}
	
	/**
	 * *视频
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	@RequestMapping(value="videoDetail/{id}")
	public ModelAndView videoDetail(@PathVariable("id") Long id){
		ModelAndView model = new ModelAndView();
		model.setViewName(MODEL_PATH+"videoDetail");
		model.addObject("webPage", "videoDetail");
		Map<String,Object> t = videoService.getInfo(id);
		model.addObject("detailInfo",t);
		model.addObject("detailName",t.get("title"));
		addPublicInfo(model,5);
		return model;
	}
	
	/**
	 * 新增留言
	 * @param tmap
	 * @return
	 * 2018年12月5日
	 * @author yc
	 */
	@ResponseBody
	@RequestMapping(value="saveLeavingMessage",method=RequestMethod.POST)
	public Result saveLeavingMessage(LeavingMessage t) {
		t.setCompanyId(companyId);
		return leavingMessageService.saveObj(t);
	}

	/**
	 * *增加页面通用信息
	 * @param model
	 * 2019年3月16日
	 * @author yc
	 */
	private void addPublicInfo(ModelAndView model,Integer pageIndex) {
		CompanyInfo info = companyInfoService.getNewEstCompanyInfoByCompanyId(companyId);
		model.addObject("companyInfo", null == info ? new HashMap<String,Object>() : info);
		model.addObject("pageIndex", pageIndex);
		
		Map<String,Object> tmap = new HashMap<String,Object>();
		tmap.put("companyId", companyId);
		model.addObject("productTypes", productTypeService.queryAll(tmap));
	}
}
